/* This file has been modified by Rohit Khare, 10/31/94 */
/*
 * Copyright (c) 1992 Athena Design, Inc. and David Pollak  
 *
 *			ALL RIGHTS RESERVED       
 * 
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the company (Athena Design) nor the names of its 
 *    employees may be used to endorse or promote products derived from this 
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ATHENA DESIGN ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ATHENA DESIGN BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#import "DPFontPanel.h"
#import "FontWell.h"
#import "ENXCP.h"

#import <appkit/Window.h>
#import <appkit/View.h>
#import <appkit/Application.h>
#import <libc.h>
#import <appkit/FontManager.h>
#import <appkit/NXBrowser.h>
#import <appkit/NXBrowserCell.h>
#import <appkit/TextField.h>
#import <appkit/Font.h>
#import <appkit/publicWraps.h>
#import <objc/List.h>
#import <dpsclient/psops.h>
#import <dpsclient/wraps.h>
#import <appkit/Panel.h>


static id doHitTest(id rootView, const NXPoint *aPoint)
{
    id subviews, subview;
    int i;
    NXPoint point;
    NXRect rect;

    point = *aPoint;
    [rootView convertPoint:&point fromView:nil];
    [rootView getBounds:&rect];
    if (NXMouseInRect(&point, &rect, [rootView isFlipped])) {
	subviews = [rootView subviews];
	for (i = [subviews count] - 1; i >= 0; i--) {
	    subview = [subviews objectAt:i]; 
	    if ((subview = doHitTest(subview, aPoint)))
		return subview;
	}
	return rootView;
    } else
    	return nil;
}

@implementation DPFontPanel

+ new
{
	DPFontPanel *dfp;
	id tid,tid2;
	NXRect re;
		
	dfp = [super new];
	tid2 = dfp -> preview;
	//tid2 = dfp -> current;
	[tid2 getBounds:&re];
	tid = [[FontWell alloc] initFrame:&re];
	[tid setAutosizing:NX_WIDTHSIZABLE | NX_HEIGHTSIZABLE];
	[tid2 setAutoresizeSubviews:YES];
	[tid setFontObj:&(dfp -> selFont)];
	[tid setLastPreview:&(dfp -> lastPreview)];
	[tid setAction:@selector(changeFont:)];
	[tid setAction:NULL];
	[tid setTarget:NULL];
	
	[tid2 addSubview:tid];
	[tid2 display];
	return dfp;
}

+ dragFont:font withEvent:(NXEvent *)ev fromView:view
{
    id		imageWindow,localWindow,tv,myView;
    NXRect	windowRect,rect;
    NXPoint	mouseDownPoint,currentPoint,upPoint;
	int		mask;
	unsigned int hitWindow,wNum;
    float	dx,dy;

	if ([font matrix] != NX_IDENTITYMATRIX)
		font = [Font newFont:[font name] size:[font pointSize] matrix:NX_IDENTITYMATRIX];

	[[view window] convertBaseToScreen:&(ev -> location)];
    // added upPoint initialization to cure dps bug if there was a quick click and no drag
	upPoint = mouseDownPoint = ev->location;
    
    windowRect.origin = ev -> location;
	windowRect.size.width = windowRect.size.height = 16.0;
	windowRect.origin.x -= 8.0;
	windowRect.origin.y -= 8.0;
    
  /* create an image window in the same location as our image */
    imageWindow = [[Window alloc] initContent:&windowRect
				  style:NX_PLAINSTYLE
				  backing:NX_BUFFERED
				  buttonMask:0
				  defer:YES];
	
			  
    [imageWindow orderFront:NULL];
	PSsetwindowlevel(NX_MAINMENULEVEL + 5,[imageWindow windowNum]);
	[imageWindow orderWindow:NX_ABOVE relativeTo:0];

	rect.origin.x = rect.origin.y = 0.0;
	rect.size.height = rect.size.width = 16.0;
	myView = [[View alloc] initFrame:&rect];
	[[imageWindow setContentView:myView]free];
	[myView lockFocus];
	NXSetColor(NX_COLORWHITE);
	PSrectfill(0.,0.,16.,16.);
	NXSetColor(NX_COLORBLACK);
	PSrectstroke(0.,1.,15.,15.);
	PSmoveto(2,3);
	[font set];
	PSshow("F");
	PSflush();
	[myView unlockFocus];
	[imageWindow flushWindow];
	NXPing();
    
	dx = (NX_X(&windowRect) - mouseDownPoint.x);
	dy = (NX_Y(&windowRect) - mouseDownPoint.y);
  /* get mouse-dragged events also (and save original mask for later) */
    mask = [[view window] addToEventMask:NX_MOUSEDRAGGEDMASK];
    
    do {
      /* wait for the next important event */
		ev = [NXApp getNextEvent:NX_MOUSEDRAGGEDMASK|NX_MOUSEUPMASK];
      
	      /* move the window to its new position */
		[[view window] convertBaseToScreen:&(ev -> location)];
		if (ev->type == NX_MOUSEDRAGGED) upPoint = ev -> location;
		currentPoint = ev->location;
		[imageWindow moveTo:(currentPoint.x + dx) :(currentPoint.y + dy)];
			
    	  /* convert the current mouse location to screen coordinates */
      
      
    } while (ev->type == NX_MOUSEDRAGGED);
    
  /* no longer need mouse dragged events, so reset the event mask */
    [[view window] setEventMask:mask];

    [[imageWindow orderOut:self] free];
    
	PSWfindownedwindow(upPoint.x,upPoint.y,&mouseDownPoint.x,&mouseDownPoint.y,&hitWindow);

	if (hitWindow) {
		NXConvertGlobalToWinNum(hitWindow,&wNum);
		localWindow = [NXApp findWindow:wNum];
		tv = doHitTest([localWindow contentView], &mouseDownPoint);
		if (tv != view && [tv respondsTo:@selector(acceptFont:atPoint:)]) {
			[tv acceptFont:font atPoint:&mouseDownPoint];
			[[tv window] makeKeyWindow];
			}
	}	
    return self;
}

- free {return self;}
@end
